package com.job.dao.Impl;

import com.job.dao.CityDao;
import com.job.model.City;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;

import java.util.List;

/**
 * @author hasee
 */
public class CityDaoImpl implements CityDao {
   private SessionFactory sessionFactory;

    /**
     * 获取所有的城市
     *
     * @return
     */
    @Override
    public List<City> getAllCity() {
         Session session=sessionFactory.openSession();
         Query query=  session.createQuery("from City ");
     return   query.getResultList();
    }

    /**
     * 获取省名 parentId=0
     *
     * @return
     */
    @Override
    public List<City> getCityParentId0() {
        Session session= sessionFactory.openSession();
       Query query= session.createQuery("from City as c where c.parentId=0");
      return   query.getResultList();

    }

    /**
     * 根据id 获取城市
     *
     * @param cityId
     * @return
     */
    @Override
    public List<City> getCity(int cityId) {
        Session session=sessionFactory.openSession();
       Query query= session.createQuery("from City as c where c. cityId=:id");
       query.setParameter("id",cityId);
       try {
           query.getComment();
           List city=  query.list();
           return city;
       }catch (Exception e){
           System.out.println("根据cityId 获取city为失败");
           e.printStackTrace();
           return null;
       }


    }

    @Override
    public List<City> getCity(String cityName) {
        Session session=sessionFactory.openSession();
        return null;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
